/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.chl.dat076.datesite.entities;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

/**
 *
 * @author Daniel
 */
@Entity
public class Message implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    @OneToOne
    private SiteUser sender;
	@Column(nullable = false)
    private boolean readd;    
    @Temporal(TemporalType.TIMESTAMP)
	@Column(nullable = false)
    private Date sent;
	@Column(nullable = false, length = 2000)
    private String content;

	public Message() {
	}

	public Message(SiteUser sender, boolean readd, Date sent, String content) {
		this.sender = sender;
		this.readd = readd;
		this.sent = sent;
		this.content = content;
	}

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public boolean isRead() {
        return readd;
    }

    public void setRead(boolean read) {
        this.readd = read;
    }

    public SiteUser getSender() {
        return sender;
    }

    public void setSender(SiteUser sender) {
        this.sender = sender;
    }

    public Date getSent() {
        return sent;
    }
    
    /**
     * @return sent in a human readable format.
     */
    public String getDateString() {
        String DATE_FORMAT = "yyyy-MM-dd HH:mm";
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
        return sdf.format(sent);
    }

    public void setSent(Date sent) {
        this.sent = sent;
    }
  
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Message)) {
            return false;
        }
        Message other = (Message) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        
        return new StringBuilder(sent.toString()).append("\n").append(sender)
                .append("\n").append(content).toString();
    }
    
}
